package com.spring.wechat.web.controller;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.spring.nbrbt.tools.otherutil.ValidatorResultHandler;
import com.spring.security.core.validators.Insert;
import com.spring.security.core.validators.Update;
import com.spring.security.web.model.BaseResult;
import com.spring.wechat.web.service.StoryService;
import com.wechat.api.web.model.StoryAlbum;
import com.wechat.api.web.model.StoryAlbumDetail;
import com.wechat.api.web.model.StoryAlbumRequest;
import com.wechat.api.web.model.StoryComment;
import com.wechat.api.web.model.StoryCommentPraise;
import com.wechat.api.web.model.StoryPlayHistory;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value="/story")
public class StoryController {

	@Resource private StoryService storyService;
	
	@RequestMapping(value = "/comment", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "评论", httpMethod = "POST", response = Object.class, notes = "评论",produces = "application/json; charset=utf-8")    
    public Object comment(
    		@RequestBody @Validated(Insert.class) StoryComment storyComment,
    		HttpServletRequest request,BindingResult result
    		) throws Exception{
		
		BaseResult rt = ValidatorResultHandler.handleResult(result);
		
		if("0".equalsIgnoreCase(rt.getStatus())){
			return rt;
		}

    	try{
    		rt = storyService.comment(storyComment);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	

	@RequestMapping(value = "/comment", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "评论列表", httpMethod = "GET", response = Object.class, notes = "评论列表",produces = "application/json; charset=utf-8")    
    public Object comment(
    		@ApiParam(required = false, name = "storyID", value = "点赞id")@RequestParam(value="storyID",required = false)String storyID
    		) throws Exception{		
		BaseResult rt = null;
    	try{
    		rt = storyService.comment(storyID);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	@RequestMapping(value = "/delcomment", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除评论", httpMethod = "POST", response = Object.class, notes = "删除评论",produces = "application/json; charset=utf-8")    
    public Object delcomment(
    		@ApiParam(required = false, name = "commentID", value = "点赞id")@RequestParam(value="commentID",required = true)String commentID,
    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token			    			    		
    		) throws Exception{		
		BaseResult rt = null;
    	try{
    		rt = storyService.delcomment(commentID,token);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	
	@RequestMapping(value = "/likeComment", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "评论点赞", httpMethod = "POST", response = Object.class, notes = "评论点赞",produces = "application/json; charset=utf-8")    
    public Object likeComment(
    		@RequestBody @Validated(Insert.class) StoryCommentPraise storyCommentPraise,
    		HttpServletRequest request,BindingResult result
    		) throws Exception{
		
		BaseResult rt = ValidatorResultHandler.handleResult(result);
		
		if("0".equalsIgnoreCase(rt.getStatus())){
			return rt;
		}

    	try{
    		rt = storyService.likeComment(storyCommentPraise);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }

		@RequestMapping(value = "/cancelLikeComment", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "取消评论点赞", httpMethod = "POST", response = Object.class, notes = "取消评论点赞",produces = "application/json; charset=utf-8")    
	    public Object cancelLikeComment(
	    		@ApiParam(required = false, name = "commentID", value = "评论id")@RequestParam(value="commentID",required = false)String commentID,
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    		
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.cancelLikeComment(token, commentID);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/album", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "添加相册", httpMethod = "POST", response = Object.class, notes = "添加相册",produces = "application/json; charset=utf-8")    
	    public Object album(
	    		@ApiParam(required = false, name = "name", value = "名字")@RequestParam(value="name",required = false)String name,
	    		@ApiParam(required = false, name = "desc", value = "描述")@RequestParam(value="desc",required = false)String desc,
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    		
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.album(token, name, desc);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/updateAlbum", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "修改相册", httpMethod = "POST", response = Object.class, notes = "添加相册",produces = "application/json; charset=utf-8")    
	    public Object updateAlbum(
	    		@RequestBody @Validated(Update.class) StoryAlbum storyAlbum,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.updateAlbum(storyAlbum);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	
		
		@RequestMapping(value = "/album", method = RequestMethod.GET)
	    @ResponseBody
	    @ApiOperation(value = "获取相册", httpMethod = "GET", response = Object.class, notes = "获取相册",produces = "application/json; charset=utf-8")    
	    public Object album(
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    		
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.album(token);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	
		
		@RequestMapping(value = "/delAlbum", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "删除相册", httpMethod = "POST", response = Object.class, notes = "删除相册",produces = "application/json; charset=utf-8")    
	    public Object delAlbum(
	    		@ApiParam(required = false, name = "albumID", value = "专辑id")@RequestParam(value="albumID",required = false)String albumID,	    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    		
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.delAlbum(albumID, token);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	

		@RequestMapping(value = "/albumDetail", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "添加到专辑", httpMethod = "POST", response = Object.class, notes = "添加到专辑",produces = "application/json; charset=utf-8")    
	    public Object albumDetail(
	    		@RequestBody @Validated(Insert.class) StoryAlbumRequest storyAlbumRequest,
	    		HttpServletRequest request
	    		) throws Exception{
			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.albumDetail(storyAlbumRequest);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/albumDetailDeleteBefore", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "添加到专辑同时删除以前的", httpMethod = "POST", response = Object.class, notes = "添加到专辑同时删除以前的",produces = "application/json; charset=utf-8")    
	    public Object albumDetailDeleteBefore(
	    		@RequestBody @Validated(Insert.class) StoryAlbumRequest storyAlbumRequest,
	    		HttpServletRequest request
	    		) throws Exception{
			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.albumDetailDeleteBefore(storyAlbumRequest);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/albumDetail", method = RequestMethod.GET)
	    @ResponseBody
	    @ApiOperation(value = "专辑详细列表", httpMethod = "GET", response = Object.class, notes = "收藏列表",produces = "application/json; charset=utf-8")    
	    public Object albumDetail(
	    		@ApiParam(required = false, name = "storyAlbumid", value = "相册id")@RequestParam(value="storyAlbumid",required = true)String storyAlbumid,
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = true)String token,
	    		@ApiParam(required = false, name = "pageSize", value = "分页")@RequestParam(value="pageSize",required = false)String pageSize,
	    		@ApiParam(required = false, name = "pageIndex", value = "分页")@RequestParam(value="pageIndex",required = false)String pageIndex,
	    		@ApiParam(required = false, name = "skip", value = "分页")@RequestParam(value="skip",required = false)String skip,
	    		@ApiParam(required = false, name = "rows", value = "分页")@RequestParam(value="rows",required = false)String rows,
	    		HttpServletRequest request
	    		) throws Exception{
			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.albumDetail(storyAlbumid,token,pageSize,pageIndex,skip,rows);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }

		
/*		@RequestMapping(value = "/updateAlbumDetail", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "修改相册收藏内容", httpMethod = "POST", response = Object.class, notes = "添加相册",produces = "application/json; charset=utf-8")    
	    public Object updateAlbumDetail(
	    		@RequestBody @Validated(Update.class) StoryAlbumDetail storyAlbumDetail,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.updateAlbumDetail(storyAlbumDetail);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	*/
		
/*		@RequestMapping(value = "/delAlbumDetail", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "删除相册内容", httpMethod = "POST", response = Object.class, notes = "删除相册内容",produces = "application/json; charset=utf-8")    
	    public Object delAlbumDetail(
	    		@ApiParam(required = false, name = "albumID", value = "专辑id")@RequestParam(value="albumID",required = false)String albumID,	    		
	    		@ApiParam(required = false, name = "childrenStoryID", value = "故事id")@RequestParam(value="childrenStoryID",required = false)String childrenStoryID,	    		    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    		
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt;
	    	try{
	    		rt = storyService.delAlbumDetail(childrenStoryID, albumID, token);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }*/
		
		
		@RequestMapping(value = "/storyCollection", method = RequestMethod.GET)
	    @ResponseBody
	    @ApiOperation(value = "个人收藏详细", httpMethod = "GET", response = Object.class, notes = "个人收藏详细",produces = "application/json; charset=utf-8")    
	    public Object storyCollection(
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    			    			    		
	    		@ApiParam(required = false, name = "skip", value = "分页")@RequestParam(value="skip",required = false)String skip,
	    		@ApiParam(required = false, name = "rows", value = "分页")@RequestParam(value="rows",required = false)String rows,
	    		HttpServletRequest request
	    		) throws Exception{
			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.storyCollection(token,skip,rows);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/storyCollection", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "个人收藏", httpMethod = "POST", response = Object.class, notes = "个人收藏",produces = "application/json; charset=utf-8")    
	    public Object storyCollection(
	    		@ApiParam(required = false, name = "storyID", value = "故事id")@RequestParam(value="storyID",required = false)String storyID,	    			    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    			    			    		
	    		HttpServletRequest request
	    		) throws Exception{			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.storyCollection(storyID, token);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	
		
		@RequestMapping(value = "/delStoryCollection", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "个人收藏", httpMethod = "POST", response = Object.class, notes = "个人收藏",produces = "application/json; charset=utf-8")    
	    public Object delStoryCollection(
	    		@ApiParam(required = false, name = "storyCollectionID", value = "故事id")@RequestParam(value="storyCollectionID",required = false)String storyCollectionID,	    			    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    			    			    		
	    		HttpServletRequest request
	    		) throws Exception{			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.delStoryCollection(storyCollectionID, token);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	
		
		@RequestMapping(value = "/playHistory", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "插入播放历史", httpMethod = "POST", response = Object.class, notes = "插入播放历史",produces = "application/json; charset=utf-8")    
	    public Object playHistory(
	    		@RequestBody @Validated(Insert.class)StoryPlayHistory storyPlayHistory,			    			    		
	    		HttpServletRequest request
	    		) throws Exception{			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.playHistory(storyPlayHistory);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/playHistory", method = RequestMethod.GET)
	    @ResponseBody
	    @ApiOperation(value = "播放历史", httpMethod = "GET", response = Object.class, notes = "播放历史",produces = "application/json; charset=utf-8")    
	    public Object playHistory(    			    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    
	    		@ApiParam(required = false, name = "skip", value = "分页")@RequestParam(value="skip",required = false)String skip,	    
	    		@ApiParam(required = false, name = "rows", value = "分页")@RequestParam(value="rows",required = false)String rows,	    	    		
	    		HttpServletRequest request
	    		) throws Exception{			
			BaseResult rt;
			
	    	try{
	    		rt = storyService.playHistory(token,skip,rows);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }	
		
		@RequestMapping(value = "/delPlayHistory", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "插入播放历史", httpMethod = "POST", response = Object.class, notes = "插入播放历史",produces = "application/json; charset=utf-8")    
	    public Object delPlayHistory(
	    		@ApiParam(required = false, name = "storyID", value = "故事id")@RequestParam(value="storyID",required = false)String storyID,	    			    		
	    		@ApiParam(required = false, name = "token", value = "用户session")@RequestParam(value="token",required = false)String token,	    			    			    		
	    		HttpServletRequest request
	    		) throws Exception{			
			BaseResult rt;			
	    	try{
	    		rt = storyService.delPlayHistory(storyID, token)/*playHistory(storyID, token)*/;
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		
		@RequestMapping(value = "/uploadVoiceByFile", method = RequestMethod.POST)
		@ApiOperation(value = "声音单次上传", httpMethod = "POST", response = Object.class, 
			notes = "声音单次上传",produces = "application/json; charset=utf-8")
		@ResponseBody
		public Object uploadVoiceByFile(HttpServletRequest req, HttpServletResponse resp,
				@ApiParam(required = true, name = "token", value = "用户token") @RequestParam(value="token",required = false) String token,
				@ApiParam(required = true, name = "durationTime", value = "时长") @RequestParam String durationTime,				
				@RequestParam(value = "voicefile", required = true) MultipartFile voicefile)
				throws Exception {

			HashMap<String,Object> rt=null;
			
	    	try{
	    		rt = storyService.commitFile("weixin/comments/",durationTime, voicefile);
	    	}catch(Exception e){
	    		//rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
			
		}

		@RequestMapping(value = "/userDataCount", method = RequestMethod.GET)
		@ApiOperation(value = "用户数据", httpMethod = "GET", response = Object.class, 
			notes = "用户数据",produces = "application/json; charset=utf-8")
		@ResponseBody
		public Object userCount(HttpServletRequest req, HttpServletResponse resp,
				@ApiParam(required = true, name = "token", value = "用户token") @RequestParam(value="token",required = false) String token
				)throws Exception {

				BaseResult rt;				
		    	try{
		    		rt = storyService.userCount(token);
		    	}catch(Exception e){
		    		rt=new BaseResult("0","系统异常");
		    		e.printStackTrace();
		    	}
		        return rt;			
		}
		
		@RequestMapping(value = "/storyHost", method = RequestMethod.POST)
		@ApiOperation(value = "主播收藏", httpMethod = "POST", response = Object.class, 
			notes = "主播收藏",produces = "application/json; charset=utf-8")
		@ResponseBody
		public Object storyHost(HttpServletRequest req, HttpServletResponse resp,
				@ApiParam(required = true, name = "hostID", value = "主播id") @RequestParam(value="hostID",required = false) String hostID,
				@ApiParam(required = true, name = "token", value = "用户token") @RequestParam(value="token",required = false) String token
				)throws Exception {

				BaseResult rt;				
		    	try{
		    		rt = storyService.storyHost(hostID, token);
		    	}catch(Exception e){
		    		rt=new BaseResult("0","系统异常");
		    		e.printStackTrace();
		    	}
		        return rt;			
		}
		
		@RequestMapping(value = "/storyHost", method = RequestMethod.GET)
		@ApiOperation(value = "用户收藏主播", httpMethod = "GET", response = Object.class, 
			notes = "用户收藏主播",produces = "application/json; charset=utf-8")
		@ResponseBody
		public Object storyHost(HttpServletRequest req, HttpServletResponse resp,
				@ApiParam(required = true, name = "token", value = "用户token") @RequestParam(value="token",required = false) String token,
				@ApiParam(required = false, name = "skip", value = "分页")@RequestParam(value="skip",required = false)String skip,	    
	    		@ApiParam(required = false, name = "rows", value = "分页")@RequestParam(value="rows",required = false)String rows    	    			    		
				)throws Exception {

				BaseResult rt;				
		    	try{
		    		rt = storyService.storyHost(token,skip,rows);
		    	}catch(Exception e){
		    		rt=new BaseResult("0","系统异常");
		    		e.printStackTrace();
		    	}
		        return rt;			
		}
		
		@RequestMapping(value = "/delStoryHost", method = RequestMethod.POST)
		@ApiOperation(value = "删除主播", httpMethod = "POST", response = Object.class, 
			notes = "删除主播",produces = "application/json; charset=utf-8")
		@ResponseBody
		public Object delStoryHost(HttpServletRequest req, HttpServletResponse resp,
				@ApiParam(required = true, name = "token", value = "用户token") @RequestParam(value="token",required = false) String token,
				@ApiParam(required = true, name = "hostCollectionID", value = "hostCollectionID") @RequestParam(value="hostCollectionID",required = false) String hostCollectionID				
				)throws Exception {

				BaseResult rt;				
		    	try{
		    		rt = storyService.delStoryHost(hostCollectionID, token);
		    	}catch(Exception e){
		    		rt=new BaseResult("0","系统异常");
		    		e.printStackTrace();
		    	}
		        return rt;			
		}

}
